package com.etime.shycourse.controller;

import com.etime.shy.common.BaseMessage;
import com.etime.shy.common.ListMessage;
import com.etime.shy.common.PojoMessage;
import com.etime.shycourse.dto.LessonAndMediaBankDto;
import com.etime.shycourse.dto.LessonAndMediaBankDtos;
import com.etime.shycourse.pojo.Chapter;
import com.etime.shycourse.pojo.Lesson;
import com.etime.shycourse.pojo.Mediabank;
import com.etime.shycourse.services.interfaces.LessonServices;
import com.etime.shycourse.services.interfaces.MediaBankServices;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author wb
 * @since 2021-12-22
 */

@Api(tags = "课时管理数据接口")
@RestController
@RequestMapping("/sys/lesson")
public class LessonController {
    @Autowired
    private LessonServices service;

    @Autowired
    private MediaBankServices mServices;

    /**
     * 添加一条数据 课时
     *
     * @param lesson 默认数据
     *               lessonuserid 课时发布人id = 登录用户的id
     *               lessonusertruename 课时发布人姓名 = 登录用户的姓名
     *               lessoncreatetime 课时创建时间 = 当前时间
     *               lessonupdatetime 课时最后修改时间 = 当前时间
     *               lessonviewnum 课时查看次数 = 0
     *               lessonbuynum 课时购买次数 =0
     *               lessonstate 课时状态 =1 （默认发布）
     *               lessontriable = 0 （目前不开放试听功能）
     *               lessontrylen 允许试听时长 = 0
     * @return int
     */
    @ApiOperation("增加课时功能 -文本  改为token")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "已改为token 测试时 必传值:登录用户的id", name = "lessonuserid"),
            @ApiImplicitParam(value = "默认值 课时创建时间 = 当前时间", name = "lessoncreatetime"),
            @ApiImplicitParam(value = "默认值 课时最后修改时间 = 当前时间", name = "lessonupdatetime"),
            @ApiImplicitParam(value = "默认值 课时查看次数 = 0", name = "lessonviewnum"),
            @ApiImplicitParam(value = "默认值 课时购买次数 =0", name = "lessonbuynum"),
            @ApiImplicitParam(value = "默认值 课时状态 =1 （默认发布）", name = "lessonstate"),
            @ApiImplicitParam(value = "默认值 试听=0 （目前不开放试听功能）", name = "lessontriable"),
            @ApiImplicitParam(value = "默认值 允许试听时长 = 0", name = "lessontrylen"),
    })
    @PostMapping("/insert")
    public BaseMessage insertLesson(Lesson lesson, HttpServletRequest request) {
        String token = request.getHeader ("token");
        // 改为token 2022 - 01 - 21 17:08:13
        int rtn = service.insertLesson (lesson, token);
        BaseMessage msg = new BaseMessage ();
        if (rtn > 0) {
            msg.setCode (rtn);
            msg.setMsg ("增加课时成功");
        } else {
            msg.setCode (0);
            msg.setMsg ("增加课时失败");
        }
        return msg;
    }

    @ApiOperation("根据课程id 查询所有其课程中所的课时(List) 文本+视频")
    @GetMapping("/list/{chapterId}")
    public ListMessage<LessonAndMediaBankDtos> selectLessonList(@PathVariable Integer chapterId) {
        List<LessonAndMediaBankDtos> list = service.selectLessonList (chapterId);
        ListMessage<LessonAndMediaBankDtos> msg = new ListMessage<> ();
        if (list != null) {
            msg.setCode (1);
            msg.setDatalist (list);
        } else {
            msg.setCode (0);
            msg.setMsg ("没有课时数据");
        }
        return msg;
    }

    @ApiOperation("根据课时id查询课时-查询一条文本+视频,课时预览（文本、视频）-数据接口")
    @GetMapping("/get/{lessonId}")
    public PojoMessage<LessonAndMediaBankDto> selectLessonAndMediaBankDtoBy(@PathVariable Integer lessonId) {
        LessonAndMediaBankDto data = service.selectLessonAndMediaBankDtoBy (lessonId);
        PojoMessage<LessonAndMediaBankDto> msg = new PojoMessage<> ();
        if (data != null) {
            msg.setCode (1);
            msg.setData (data);
        } else {
            msg.setCode (0);
            msg.setMsg ("没有找到课时数据");
        }
        return msg;
    }

    @ApiOperation("根据课程id查询 查询章节和课时的详细信息")
    @ApiImplicitParam("根据课程id 用于查询到章节-课时的详细信息")
    @GetMapping("/List/gets/{CourseId}")
    public ListMessage<Chapter> selectCourseLessonById(@PathVariable Integer CourseId) {
        List<Chapter> data = service.selectCourseLessonByIds (CourseId);
        ListMessage<Chapter> msg = new ListMessage<> ();
        if (data != null) {
            msg.setCode (1);
            msg.setDatalist (data);
        } else {
            msg.setCode (0);
            msg.setMsg ("没有课时数据");
        }
        return msg;
    }


    @ApiOperation("完善(修改)课时信息接口 -文本 (必传值 课时的id)其他的字段，根据前端的需要传值")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "lessonid", value = "必传值 课时的id"),
            @ApiImplicitParam("其他的字段，根据前端的需要传值"),
    })
    @PostMapping("/update")
    public BaseMessage updateLessonById(Lesson lesson) {
        int rtn = service.updateLessonById (lesson);
        BaseMessage msg = new BaseMessage ();
        if (rtn > 0) {
            msg.setCode (rtn);
            msg.setMsg ("修改课时信息成功");
        } else {
            msg.setCode (0);
            msg.setMsg ("修改课时信息失败");
        }
        return msg;
    }

    @ApiOperation("增加课时功能 -文本+视频")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "可不传 默认值 课时创建时间 = 当前时间", name = "lesson.lessoncreatetime"),
            @ApiImplicitParam(value = "可不传 默认值 课时最后修改时间 = 当前时间", name = "lesson.lessonupdatetime"),
            @ApiImplicitParam(value = "可不传 默认值 课时查看次数 = 0", name = "lesson.lessonviewnum"),
            @ApiImplicitParam(value = "可不传 默认值 课时购买次数 =0", name = "lesson.essonbuynum"),
            @ApiImplicitParam(value = "可不传 默认值 课时状态 =1 （默认发布）", name = "lesson.lessonstate"),
            @ApiImplicitParam(value = "可不传 默认值 试听=0 （目前不开放试听功能）", name = "lesson.lessontriable"),
            @ApiImplicitParam(value = "可不传 默认值 允许试听时长 = 0", name = "lesson.lessontrylen"),

            @ApiImplicitParam(value = "文件名称 源文件的名称", name = "mediabank.mediatitle"),
            @ApiImplicitParam(value = "文件类型 保存源文件的后缀名", name = "mediabank.mediatype"),
            @ApiImplicitParam(value = "文件路径 文件上传后，返回的路径", name = "mediabank.mediaurl"),
            @ApiImplicitParam(value = "播放时间 视频的播放时长", name = "mediabank.medialen"),
            @ApiImplicitParam(value = "文件创建时间 默认值 当前时间", name = "mediabank.mediacreatetime"),
            @ApiImplicitParam(value = "不传 文件上传人id 当前登录人id", name = "mediabank.mediauserid"),
            @ApiImplicitParam(value = "不传 文件上传人姓名 当前登录人姓名", name = "mediabank.mediausertruename"),
            @ApiImplicitParam(value = "可不传 文件状态 默认：0(待转码)", name = "mediabank.mediastate"),
            @ApiImplicitParam(value = "可不传 文件删除状态 默认：0（正常）", name = "mediabank.mediadelete"),
            @ApiImplicitParam(value = "可不传 文文件审核状态 默认：0（未审核）", name = "mediabank.mediaauditstate"),
            @ApiImplicitParam(value = "不传 默认值 空 件审核意见", name = "mediabank.mediaauditdesc"),
            @ApiImplicitParam(value = "不传 默认值 空 文件审核人id", name = "mediabank.mediaaudituserid"),
            @ApiImplicitParam(value = "不传 默认值 空 文文件审核人姓名", name = "mediabank.mediaauditname")
    })
    @PostMapping("/inserts")
    public BaseMessage insertMediaBankAndLesson(LessonAndMediaBankDto dto, HttpServletRequest request) {
        String token = request.getHeader ("token");
        //  改为token
        int rtn = service.insertMediaBankAndLesson (dto, token);
        BaseMessage msg = new BaseMessage ();

        if (rtn > 0) {
            msg.setCode (rtn);
            msg.setMsg ("增加课时成功");
        } else {
            msg.setCode (0);
            msg.setMsg ("增加课时失败");
        }
        return msg;
    }

    /**
     * 根据课时id修改 dto.lessonid
     * 根据视频id修改 dto.mediaid
     *
     * @param dto
     * @return int
     */
    @ApiOperation("完善(修改)课时信息接口-修改课时文本+多媒体文件信息 (必传值 课时的id,视频的id)其他的字段，根据前端的需要传值")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "lesson.lessonid", value = " 必传值 课时的id"),
            @ApiImplicitParam(name = "mediabank.mediaid", value = "必传值 视频的id"),
            @ApiImplicitParam("其他的字段，根据前端的需要传值"),
    })
    @PostMapping("/updates")
    public BaseMessage updateLessonMediaBankById(LessonAndMediaBankDto dto) {
        int rtn = service.updateLessonMediaBankById (dto);
        BaseMessage msg = new BaseMessage ();
        if (rtn > 0) {
            msg.setCode (rtn);
            msg.setMsg ("修改课时信息成功");
        } else {
            msg.setCode (0);
            msg.setMsg ("修改课时信息失败");
        }
        return msg;
    }

    @ApiOperation("插入 完善(修改)多媒体文件信息:1、修改 必传值 mediaid其他的字段,根据前端的需要传值; 2、没有传入mediaid 便插入一填数据 返回其id")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mediaid", value = "修改时 必传值 多媒体文件的id"),
    })
    @PostMapping("/MediaBank/updateOrInsert")
    public BaseMessage updateOrInsetMediaBankBy(Mediabank mediabank) {
        int rtn = mServices.updateOrInsertMediaBankBy (mediabank);
        BaseMessage msg = new BaseMessage ();
        if (rtn > 0) {
            msg.setCode (rtn);
            msg.setMsg ("修改或增加多媒体文件信息成功");
        } else {
            msg.setCode (0);
            msg.setMsg ("修改或增加多媒体文件信息失败");
        }
        return msg;
    }

    @ApiOperation("根据课时id 物理删除课时 删除成功返回=1 失败=返回0")
    @PostMapping("/delete/ById/{LessonId}")
    public BaseMessage deleteLessonById(@PathVariable Integer LessonId) {
        int rtn = service.deleteLessonById (LessonId);
        BaseMessage msg = new BaseMessage ();
        if (rtn > 0) {
            msg.setCode (rtn);
            msg.setMsg ("删除课时信息成功");
        } else {
            msg.setCode (0);
            msg.setMsg ("删除课时信息失败");
        }
        return msg;
    }
}
